package com.deyuanyun.pic.settings.service.base.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.domain.base.BizMsgSend;
import com.deyuanyun.pic.settings.domain.base.BizMsgSendWithBLOBs;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.mapper.base.BizMsgSendMapper;
import com.deyuanyun.pic.settings.mapper.base.BizMsgUserExpandMapper;
import com.deyuanyun.pic.settings.mapper.base.BizMsgUserMapper;
import com.deyuanyun.pic.settings.service.base.BizMsgSendService;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

@Service
public class BizMsgSendServiceImpl implements BizMsgSendService {
    @Autowired
    private BizMsgSendMapper bizMsgSendMapper;

    @Autowired
    private BizMsgUserMapper bizMsgUserMapper;

    @Autowired
    private BizMsgUserExpandMapper bizMsgUserExpandMapper;

    private static final Logger logger = LoggerFactory.getLogger(BizMsgSendServiceImpl.class);

    public BizMsgSend selectByPrimaryKey(String id) {
        return this.bizMsgSendMapper.selectByPrimaryKey(id);
    }

    public int deleteByPrimaryKey(String id) {
        return this.bizMsgSendMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(BizMsgSendWithBLOBs record) {
        return this.bizMsgSendMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(BizMsgSend record) {
        return this.bizMsgSendMapper.updateByPrimaryKey(record);
    }

    public int insert(BizMsgSendWithBLOBs record) {
        return this.bizMsgSendMapper.insert(record);
    }

    @Override
    public int insertSelective(BizMsgSendWithBLOBs record) {
        return this.bizMsgSendMapper.insertSelective(record);
    }

    @Override
    public List<BizMsgUser> queryMsgListByUser() {
        UserSessionVO userSessionVO = SessionCacheSupport.getSessionVO();
        String userId = userSessionVO.getId();
        List<BizMsgUser> list = bizMsgUserExpandMapper.queryMsgListByUser(userId);
        if(list != null && list.size() > 0) {
            for (BizMsgUser msgUserDTO : list) {//循环组装成需要展示的消息
                String msgContent = msgUserDTO.getMsgContent();
                String thresholdContent = msgUserDTO.getThresholdContent();
                if (StringUtils.isNotEmpty(msgContent) && StringUtils.isNotEmpty(thresholdContent)) {
                    String[] thresholdStrs = thresholdContent.split(",");
                    if (thresholdStrs != null && thresholdStrs.length > 0) {
                        String newMsgContent = MessageFormat.format(msgContent, thresholdStrs);
                        msgUserDTO.setMsgContent(newMsgContent);
                        msgUserDTO.setThresholdContent("");
                    }
                }
            }
        }
        return list;
    }
}